// Copyright (c) Microsoft Corporation. 
// Licensed under the MIT license.

#include "SimplygonProcessor.h"
#include "Misc/ScopeLock.h"

bool FSimplygonProcessor::Tick(float /*DeltaTime*/)
{
	FScopeLock Lock(&StateLock);

	for (auto& Job : JobsToComplete)
	{
		Job.ExecuteIfBound();
	}
	JobsToComplete.Empty();
	
	return true;
}

void FSimplygonProcessor::Add(const FSimplygonProcessGameThreadDelegate& ExecuteOnGameThread)
{
	if (IsInGameThread())
	{
		ExecuteOnGameThread.ExecuteIfBound();
		return;
	}
	{
		FScopeLock Lock(&StateLock);
		JobsToComplete.Add(ExecuteOnGameThread);
	}
	while (1)
	{
		FScopeLock Lock(&StateLock);
		if (!JobsToComplete.Num())
			return;
	}

}
